package com.example.vertify

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.tamsiree.rxkit.RxTool.init

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(ActivityTracker)
        init(this)
    }

    object ActivityTracker : ActivityLifecycleCallbacks {
        private val activities = mutableListOf<Activity>()

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            activities.add(activity)
        }

        override fun onActivityDestroyed(activity: Activity) {
            activities.remove(activity)
        }

        // 其他生命周期方法留空
        override fun onActivityStarted(activity: Activity) {}
        override fun onActivityResumed(activity: Activity) {}
        override fun onActivityPaused(activity: Activity) {}
        override fun onActivityStopped(activity: Activity) {}
        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

        fun finishActivity(cls: Class<*>) {
            activities.firstOrNull { it.javaClass == cls }?.finish()
        }
    }
}
